// pagesEnterprise/pages/newsInformationDetail/newsInformationDetail.js
import ajax from "../../../utils/index"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    housingList: [],
    companyData: null
  },
  /**
       * 打开地图
       */
  locationEvent(event) {
    console.log(event)
    // openMmap(this.data.item.location)
  },
  /**
   * 接口 - 获取精品房源 企业信息
   */
  async get_article_info_housing() {
    const res = await ajax.get('/wx/appletHousing/articleInfoHousing')
    if (res.code != 200) return;
    this.setData({ housingList: res.rows })
  },
  /**
   * 接口 - 获取 用户的企业信息
   */
  async get_applet_company_data() {
    const userInfo = wx.getStorageSync('userInfo')
    if (!userInfo || !userInfo.id) return;
    const res = await ajax.get('/wx/appletCompanyData/info',{
      userId: userInfo.id
    })
    if (res.code != 200) return;
    const company = res.data.data
    company.establishTime = company.establishTime ? dayjs(company.establishTime).format('YYYY年MM月DD日') : ''
    this.setData({
      companyData: company,
      userInfo: userInfo
    })
  },
  /**
   * 接口 - 获取文章详情
   */
  get_artcle_detail: function (id) {
    ajax.get(`/wx/AppletArticle/getInfo?id=${id}`).then(res => {
      if (res.code != 200) return
      let detail = res.data
      detail.content = detail.content.replace(/\<img/gi, '<img style="max-width:100%;height:auto"')
      this.setData({ detail: detail })
    })
  },

  /**
   * 打电话
   */
  callTel: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options && options.inviterUserId) {
      app.globalData.shareId = options.inviterUserId
      this.setData({
        userInfo: {
          nickName: options.nickName,
          wxPhone: options.wxPhone,
          headimgUrl: options.headimgUrl
        }
      })
    } else {
      this.setData({
        userInfo: wx.getStorageSync('userInfo')
      })
    }
    // this.get_article_info_housing()
    // this.get_applet_company_data()
    this.get_artcle_detail(options.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this
    const userInfo = wx.getStorageSync('userInfo')
    console.log('/pagesEnterprise/pages/newsInformationDetail/newsInformationDetail?isShare=1&id=' + that.data.detail.id + '&inviterUserId=' + userInfo.id + '&nickName=' + that.data.userInfo.nickName + '&wxPhone=' + that.data.userInfo.wxPhone + '&headimgUrl=' + that.data.userInfo.headimgUrl,)
    return {
      title: that.data.detail.title,
      path: '/pagesEnterprise/pages/newsInformationDetail/newsInformationDetail?isShare=1&id=' + that.data.detail.id + '&inviterUserId=' + userInfo.id + '&nickName=' + that.data.userInfo.nickName + '&wxPhone=' + that.data.userInfo.wxPhone + '&headimgUrl=' + that.data.userInfo.headimgUrl,
      imageUrl: that.data.detail.logoImg,
    }
  }
})